import 'package:flutter/material.dart';

class CashFlowPainter extends CustomPainter {
  final List<double> incomeData;
  final List<double> expenseData;
  final List<double> cashFlowData;

  CashFlowPainter(this.incomeData, this.expenseData, this.cashFlowData);

  @override
  void paint(Canvas canvas, Size size) {
    double barWidth = (size.width - (incomeData.length - 1) * 10) /
        incomeData.length; // 计算柱状图宽度，留出间隔

    Paint barPaint = Paint()
      ..style = PaintingStyle.fill
      ..color = Colors.green;

    Paint expenseBarPaint = Paint()
      ..style = PaintingStyle.fill
      ..color = Colors.red;

    // Paint linePaint = Paint()
    //   ..style = PaintingStyle.stroke
    //   ..color = Colors.blue
    //   ..strokeWidth = 3;

    // 绘制横坐标刻度和月份
    TextStyle textStyle = const TextStyle(color: Colors.black, fontSize: 12);
    for (int i = 0; i < incomeData.length; i++) {
      double x = i * (barWidth + 10); // 留出间隔

      // 绘制月份文本，倾斜显示
      TextSpan span = TextSpan(
        text: [
          'Jan',
          'Feb',
          'Mar',
          'Apr',
          'May',
          'Jun',
          'Jul',
          'Aug',
          'Sep',
          'Oct',
          'Nov',
          'Dec'
        ][i],
        style: textStyle,
      );
      TextPainter textPainter = TextPainter(
        text: span,
        textAlign: TextAlign.center,
        textDirection: TextDirection.ltr,
      );
      textPainter.layout();
      canvas.save();
      canvas.translate(30 + x + barWidth / 2, size.height); // 平移到对应的位置
      canvas.rotate(-0.3); // 旋转 -30度
      textPainter.paint(canvas, Offset(-textPainter.width / 2, 5)); // 绘制文本
      canvas.restore();
    }

    const double minY = -500;
    const double maxY = 2000;
    const double step = 500; // 每个刻度的间隔

    // 绘制柱状图和折线图
    for (int i = 0; i < incomeData.length; i++) {
      double incomeHeight =
          ((incomeData[i] - minY) / (maxY - minY)) * size.height * 4 / 5;

      // 绘制收入柱状图（正值）
      if (incomeData[i] >= 0) {
        canvas.drawRect(
          Rect.fromLTWH(
              i * (barWidth + 10),
              size.height - incomeHeight - size.height * 1 / 5,
              barWidth,
              incomeHeight),
          barPaint,
        );
      }

      double expenseHeight =
          ((expenseData[i] - minY) / (0 - minY)) * size.height * 1 / 5;

      // 绘制支出柱状图（负值），如果支出是负数则绘制
      if (expenseData[i] < 0) {
        canvas.drawRect(
          Rect.fromLTWH(i * (barWidth + 10), size.height * 4 / 5, barWidth,
              expenseHeight),
          expenseBarPaint,
        );
      }
    }

    // 绘制现金流折线
    // Path path = Path();
    // path.moveTo(
    //     0,
    //     size.height / 2 -
    //         ((cashFlowData[0] - minValue) / (maxValue - minValue)) *
    //             size.height);
    // for (int i = 1; i < cashFlowData.length; i++) {
    //   path.lineTo(
    //       i * (barWidth + 10),
    //       size.height / 2 -
    //           ((cashFlowData[i] - minValue) / (maxValue - minValue)) *
    //               size.height);
    // }
    // canvas.drawPath(path, linePaint);

    //-------------22222

    // 绘制纵坐标刻度值

    Paint axisPaint = Paint()
      ..color = Colors.orange
      ..strokeWidth = 0.5;

    // 绘制坐标轴
    // canvas.drawLine(Offset(0, size.height / 2),
    //     Offset(size.width, size.height / 2), axisPaint); // X轴（0轴）
    canvas.drawLine(
        const Offset(40, 0), Offset(40, size.height), axisPaint); // Y轴

    // 计算每个刻度的位置
    for (double y = minY; y <= maxY; y += step) {
      double position = (y - minY) / (maxY - minY) * size.height;
      canvas.drawLine(
        Offset(30, size.height - position),
        Offset(size.width, size.height - position),
        axisPaint,
      );

      // 绘制刻度值文本
      TextPainter(
        text: TextSpan(
          text: y.toString(),
          style: const TextStyle(color: Colors.black, fontSize: 12),
        ),
        textDirection: TextDirection.ltr,
      )
        ..layout()
        ..paint(
          canvas,
          Offset(0, size.height - position - 10),
        );
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

class CashFlowChart extends StatelessWidget {
  final List<double> incomeData = [
    1500,
    600,
    700,
    1500,
    300,
    900,
    1000,
    1200,
    1100,
    1400,
    1600,
    2000
  ];
  final List<double> expenseData = [
    -200,
    -300,
    -100,
    -200,
    -150,
    -400,
    -300,
    -350,
    -450,
    -480,
    -490,
    -500
  ];
  final List<double> cashFlowData = [
    300,
    300,
    600,
    600,
    150,
    500,
    700,
    850,
    650,
    800,
    800,
    800
  ];

  CashFlowChart({super.key}) {
    for (int i = 0; i < incomeData.length; i++) {
      cashFlowData.add(incomeData[i] - expenseData[i]); // 计算现金流
    }
  }

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(double.infinity, 250),
      painter: CashFlowPainter(incomeData, expenseData, cashFlowData),
    );
  }
}


/*

   // 绘制纵坐标刻度
    for (int i = 0; i <= 5; i++) {
      double y = size.height / 5 * i;
      canvas.drawLine(Offset(0, y), Offset(5, y), axisPaint); // 绘制刻度线
      TextSpan span = TextSpan(
        text: (minValue + (maxValue - minValue) * (i / 5)).toStringAsFixed(0),
        style: TextStyle(color: Colors.black, fontSize: 10),
      );
      TextPainter textPainter = TextPainter(
        text: span,
        textAlign: TextAlign.right,
        textDirection: TextDirection.ltr,
      );
      textPainter.layout();
      textPainter.paint(canvas, Offset(10, y - textPainter.height / 2)); // 绘制文本
    }


*/